Completed
Push — master ( a0c18c...217d14 )
by D
02:26
created

Lightbox.initHTML   A

Complexity

Conditions 1

Size

Total Lines 26
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
dl 0
loc 26
rs 9.304
c 0
b 0
f 0
1
import {define} from "../globals/globals";
2
import {LightboxInterface} from "../interfaces/LightboxInterface";
3
import {LightboxOptions} from "../types/LightboxOptions";
4
import {ImagePositionOptions} from "../types/internal/ImagePositionOptions";
5
6
(function (root, factory) {
7
    if (typeof define === 'function' && define.amd) {
8
        define([], factory);
9
    } else if (typeof module === 'object' && module.exports) {
10
        module.exports = factory();
11
    } else {
12
        root.Lightbox = factory();
13
    }
14
}(typeof self !== 'undefined' ? self : this, function () {
15
16
    class Lightbox implements LightboxInterface {
17
        public options: LightboxOptions;
18
19
        constructor(options: LightboxOptions) {
20
21
            if (!options.targets) {
22
                throw new Error('No targets')
23
            }
24
25
            this.options = options;
26
            this.options.targets = typeof options.targets === 'string' ? document.querySelectorAll(options.targets) : options.targets;
27
            this.initClasses();
28
            this.initClone();
29
            this.initClose();
30
        }
31
32
        private initClasses(): void {
33
            this.options.targets.forEach(function (target: HTMLImageElement) {
34
                target.classList.add('lightbox');
35
            });
36
        }
37
38
        private initHTML(target: HTMLImageElement, position: ImagePositionOptions): void {
39
            let wrapper = document.createElement('div');
40
            wrapper.classList.add('lightbox-clone-wrapper');
41
42
            setTimeout(() => {
43
                wrapper.classList.add('active');
44
            }, 100);
45
46
            let clone = document.createElement('div');
47
            clone.classList.add('lightbox-clone');
48
            clone.style.top = position.top + 'px';
49
            clone.style.left = position.left + 'px';
50
            clone.style.width = target.naturalWidth + 'px';
51
            clone.style.height = target.naturalHeight + 'px';
52
53
            setTimeout(() => {
54
                clone.classList.add('centered');
55
            }, 100);
56
57
            let cloneImg = document.createElement('img');
58
            cloneImg.src = target.src;
59
60
            clone.appendChild(cloneImg);
61
            wrapper.appendChild(clone);
62
            document.body.appendChild(wrapper);
63
        }
64
65
        private initClone(): void {
66
            let that = this;
67
68
            this.options.targets.forEach(function (target: HTMLImageElement) {
69
                target.addEventListener('click', function (e) {
70
                    that.initHTML(e.target as HTMLImageElement, Lightbox.getTargetPosition(target));
71
                })
72
            });
73
        }
74
75
        static getTargetPosition(target: HTMLImageElement): ImagePositionOptions {
76
            let targetPosition = target.getBoundingClientRect();
77
78
            return {
79
                top: targetPosition.top,
80
                left: targetPosition.left
81
            }
82
        }
83
84
        protected initClose(): void {
85
            this.bodyClose();
86
            this.scrollClose();
87
        }
88
89
        static close(): void {
90
            let wrapper = document.querySelector('.lightbox-clone-wrapper');
91
92
            if(wrapper) {
93
                let clone = wrapper.querySelector('.lightbox-clone')!;
94
                clone.classList.remove('centered');
95
96
                setTimeout(() => {
97
                    wrapper!.remove();
98
                }, 500);
99
            }
100
101
        }
102
103
        protected bodyClose(): void {
104
            let that = this;
105
106
            document.addEventListener('click', function (e) {
107
                e.preventDefault();
108
109
                if((e.target as HTMLElement).closest('.lightbox-clone-wrapper')) {
110
                    Lightbox.close();
111
                }
112
            });
113
        }
114
115
        protected scrollClose(): void {
116
            let that = this;
117
118
            document.addEventListener('scroll', function () {
119
                Lightbox.close();
120
            });
121
        }
122
    }
123
124
    return Lightbox;
125
}));
126
127